package com.smartwebx.ha.util;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 
 * 创建人：Dream~天涯 
 * 创建时间：2015-7-1 下午7:36:33
 * 邮箱：154494796@qq.com
 * 备注：
 */
public class ReadINI {
	private static Map<String, HashMap<String, String>> sectionsMap = new HashMap<String, HashMap<String, String>>();  
	//      item    value  
	private static HashMap<String, String> itemsMap = new HashMap<String, String>();  

	private static String currentSection = "";  
	private static int i = 0;

	/** 
	 * Load data from target file 
	 * @param in target file. It should be in ini format 
	 */  
	private static void loadData(BufferedReader in) {  
		i = 0;
		try {  
			String line = null;  
			while ((line = in.readLine()) != null) {  
				line = line.trim();  
				if("".equals(line)) continue;  
				if(line.startsWith("[") && line.endsWith("]")) {  
					// Ends last section  
					if(itemsMap.size() > 0 && !"".equals(currentSection.trim())) {  
						sectionsMap.put(currentSection, itemsMap);  
					}  
					currentSection = "";  
					itemsMap = null;  

					// Start new section initial  
					currentSection = line.substring(1, line.length() -1);  
					itemsMap = new HashMap<String, String>();   
				} else {  
					int index = line.indexOf("=");  
					int index2 = line.indexOf(";"); 
					if(index != -1&&index2<0) {  
						String key = line.substring(0,index);  
						String value = line.substring(index + 1, line.length());  
						itemsMap.put(key, value);  
						if(key.indexOf("边界文件")>=0){
							i++;
						}
					}  
				}  
				//System.out.println(line);  
			}  
			if(itemsMap.size() > 0 && !"".equals(currentSection.trim())) {  
				sectionsMap.put(currentSection, itemsMap);  
			} 
		} catch (Exception e) {  
			e.printStackTrace();  
		} 
	}  


	public static String getValue(String section, String item, BufferedReader file) {  
		loadData(file);  

		HashMap<String, String> map = sectionsMap.get(section);  
		if(map == null) {  
			return "No such section:" + section;  
		}  
		String value = map.get(item);  
		if(value == null) {  
			return "No such item:" + item;  
		}  
		return value;  
	}  

	public static Map<String, String> getValue2(String section, String item, BufferedReader in) {  
		Map<String, String> map2=new HashMap<String, String>();
		loadData(in);  
		String value = null;
		String value2 = null;
		HashMap<String, String> map = sectionsMap.get(section);  
		if(map == null) {  
			return map2;  
		}  
		for(int j=1 ; j<=i ; j++){
			value = map.get("边界文件"+j).trim();
			value2 = map.get("面平均"+j).trim();
			if(value!=null&&!"".equals(value)){
				map2.put(value.substring(0, value.length()-6),value2.trim());
				//System.out.println("边界文件"+j+":"+value.substring(0, 2));
				//System.out.println("面平均"+j+":"+value2.trim());
				//System.out.println();
			}
		}

		if(value == null) {  
			return map2;  
		}  
		return map2;  
	}  

	public Map<String, String> getValue(String section, String item, String fileName) {  		
		FileInputStream fInputStream;
		BufferedReader in = null;
		try {
			BufferedInputStream bin = new BufferedInputStream(  
					new FileInputStream(fileName));  
			int p = (bin.read() << 8) + bin.read();  
			String code = null;  

			switch (p) {  
			case 0xefbb:  
				code = "UTF-8";  
				break;  
			case 0xfffe:  
				code = "Unicode";  
				break;  
			case 0xfeff:  
				code = "UTF-16BE";  
				break;  
			default:  
				code = "GBK";  
			}  

			fInputStream = new FileInputStream(fileName);
			InputStreamReader inputStreamReader = new InputStreamReader(fInputStream, code);  
			in = new BufferedReader(inputStreamReader);  
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  


		return getValue2(section, item, in);  
	}  

	public static List<String> getSectionNames(BufferedReader file) {  
		List<String> list = new ArrayList<String>();  
		loadData(file);  
		Set<String> key = sectionsMap.keySet();  
		for (Iterator<String> it = key.iterator(); it.hasNext();) {  
			list.add(it.next());  
		}  
		return list;  
	}  

	public static Map<String, String> getItemsBySectionName(String section, BufferedReader file) {  
		loadData(file);  
		return sectionsMap.get(section);  
	}  
	public static void main(String[] args) {
		/*Map<String, String> ff = getValue("区域属性","计算面平均","D://Visoline.ini");*/
		/*System.out.println(ff);*/
		//loadData(new File("D://Visoline.ini"));
	}
}
